package com.bx.scala.ObjextFoundation06

/**
 * 演示 在伴生类内调用私有属性
 */
class DemoCompanion {
  // 公共属性
  var sth = "Java"
  // 定义一个私有属性
  private val p = "private"

  def sayHello(): Unit = {
    println(s"Hello ${this.sth}")
  }
  def settSth(s :String): Unit = {
    this.sth = s"this.sth = ${s}"
  }
}

object DemoCompanion {
  var sth = "Scala"
  def sayHello():Unit = {
    println(s"Companion object, hellp ${this.sth}")
  }
  def main(args: Array[String]): Unit = {
    println(this.sth)
    this.sayHello()
    val testObject = new DemoCompanion()
    testObject.sayHello()
    println("getter: "+testObject.sth) //getter
    testObject.sth = "Golang"
    println("after setter: "+testObject.sth)
    testObject.settSth("Spark")
    println(testObject.sth)
    // 使用伴生对象 - object和class名相同时，私有属性是能被访问的
    // 如果将object DemoCompanion替换为其他的object名称，.p就不能被访问
  }
}
